package org.shiftone.cdep.store;

import org.apache.log4j.Logger;
import org.shiftone.cdep.util.JDBCUtil;
import org.shiftone.cdep.store.AbstractStoreManager;

import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class OracleStoreManager extends AbstractStoreManager
{
    public static Logger LOG = Logger.getLogger(OracleStoreManager.class);

    private String sequenceName;

    public OracleStoreManager()
    {
    }

    private static final String[] methodPrefix = { "get", "set", "drop" };

    private String getSequenceName(String methodName)
    {
        if (methodName.endsWith("Method"))
            return "METHOD_SEQ";
        else
        if (methodName.endsWith("Class"))
            return "CLASS_SEQ";
        else
        if (methodName.endsWith("ClassImplements"))
            return "CLASS_IMPLEMENTS_SEQ";
        else
        if (methodName.endsWith("MethodCall"))
            return null;
        else
        if (methodName.endsWith("ClassCall"))
            return null;
        else
        if (methodName.endsWith("ImportLocation"))
            return "IMPORT_LOCATION_SEQ";
        else
        if (methodName.endsWith("ClassImplements"))
            return "CLASS_IMPLEMENTS_SEQ";
        else
            LOG.error("Unable to find sequence for method name: " + methodName);

        return null;
    }

    protected String prepareInsertQuery(String methodName, String query, boolean generateKey)
    {
        sequenceName = getSequenceName(methodName);

        return query;
    }

    protected String prepareUpdateQuery(String methodName, String query)
    {
        return query;
    }

    protected String prepareSelectQuery(String methodName, String query)
    {
        return query;
    }

    protected String prepareDeleteQuery(String methodName, String query)
    {
        return query;
    }

    protected int executeInsert(PreparedStatement stmt, boolean generateKey) throws SQLException
    {
        if (stmt.executeUpdate() != 1)
        {
            throw new SQLException("Failed to insert row");
        }

        if (sequenceName != null)
        {
            Connection conn = getConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;

            try
            {
                ps = conn.prepareStatement("select " + sequenceName + ".CURRVAL from dual");
                rs = ps.executeQuery();
                if (rs.next())
                {
                    return rs.getInt(1);
                }
            }
            finally
            {
                JDBCUtil.close(rs);
                JDBCUtil.close(ps);
                closeConnection();
            }
        }

        return 0;
    }

    protected void executeDelete(PreparedStatement stmt) throws SQLException
    {
        if (stmt.executeUpdate() < 1)
        {
            throw new SQLException("Failed to update any rows");
        }
    }

    protected ResultSet executeSelect(PreparedStatement stmt) throws SQLException
    {
        return stmt.executeQuery();
    }

    protected void executeUpdate(PreparedStatement stmt) throws SQLException
    {
        if (stmt.executeUpdate() < 1)
        {
            throw new SQLException("Failed to update any rows");
        }
    }
}
